﻿using MT.CustomUserControl;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MT.CustomWindow
{
    /// <summary>
    /// SmallWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SmallWindow : Window
    {
        //通过2种方式王外传送确定或取消按钮按下了哪个，
        //1. 需要跨程序的则用SetPressedBtnType，
        //2. 关闭窗体直接可获取的用PressedBtnType
        private delegate void SetPressedBtnTypeEventHandler(BtnType btnType);
        private event SetPressedBtnTypeEventHandler SetPressedBtnType;
        private bool CanOkCloseWindow = false;
        public BtnType PressedBtnType = BtnType.None;

        private UserControl realContentUserControl1;
        private Visibility BtnOkVisibility = Visibility.Visible;
        private Visibility BtnCancelVisibility = Visibility.Visible;

        public static Action<bool> WindowShowAction;

        public SmallWindow()
        {
            InitializeComponent();
        }

        public SmallWindow(string tiitle, BaseUserControl userControl, bool canOkCloseWindow = false, Visibility btnOkVisibility = Visibility.Visible, Visibility btnCancelVisibility = Visibility.Visible)
        {
            InitializeComponent();
            SetUserControlRealContent(tiitle, userControl);

            CanOkCloseWindow = canOkCloseWindow;
            BtnOkVisibility = btnOkVisibility;
            BtnCancelVisibility = btnCancelVisibility;
        }

        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            if (CanOkCloseWindow)
            {
                PressedBtnType = BtnType.OK;
                this.Close();
            }
            else
            {
                SetPressedBtnType.Invoke(BtnType.OK);
            }         
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// 设置中间内容区实际的UserControl
        /// </summary>
        /// <param name="userControl"></param>
        private void SetUserControlRealContent(string tiitle, BaseUserControl userControl)
        {
            TextBlock_Title.Text = tiitle;
            realContentUserControl1 = userControl;
            SetPressedBtnType += userControl.GetParentWindowBtnType;

            grid.Children.Add(realContentUserControl1);
            Grid.SetRow(realContentUserControl1, 2);
            Grid.SetColumnSpan(realContentUserControl1, 2);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Button_OK.Visibility = BtnOkVisibility;
            Button_Cancel.Visibility = BtnCancelVisibility;
        }
    }
}
